#include "Blocks.h"
#include "utils\Log.h"
#include <stdio.h>
#include "base\Framework.h"
#include "base\Engine.h"
#include "game\World.h"
#include "utils\Timer.h"
#include "SimpleHighscoreService.h"
#include "RunState.h"
#include "HUD.h"
#include "addons\ScreenQuad.h"
#include "addons\BasicGameState.h"
#include "SimpleStates.h"
#include "Block.h"
#include "Constants.h"
#include "actions\RunLaserAction.h"
#include "actions\CheckCoverageAction.h"
#include "actions\GameActions.h"
#include "actions\MenuActions.h"
#include "action\DebugInfoAction.h"

Blocks::Blocks() : ds::CGame() {
}

void Blocks::loadContent(ds::ResManager& resourceManager) {	
	gEngine->getActionManager().startAction("UpdateBackground");
	gEngine->getActionManager().startAction("MainMenu");
}

void Blocks::initialize() {
	Logger::log("Starting game");	
	gEngine->addController(this);
	gEngine->addService(new ds::CTimer());

	gEngine->getResourceManager().loadFont("XScale","content\\fonts\\X-SCALE_.TTF","xscale",30,false);
	gEngine->getResourceManager().loadFont("BigXScale","content\\fonts\\X-SCALE_.TTF","xscale",48,false);	

	m_Core = new GameCore();
	// prepare actions
	ds::ActionManager& am = gEngine->getActionManager();
	am.addAction(new ds::DebugInfoAction());
	am.addAction(new CheckCoverageAction(m_Core));
	am.addAction(new RunLaserAction(m_Core));
	am.addAction(new UpdateBackgroundAction(m_Core));
	am.addAction(new MouseAction(m_Core));
	am.addAction(new ShowGridAction(m_Core));
	am.addAction(new MainMenuAction("TexturesMaterial","StartMenu",m_Core));
	am.addAction(new CreditsMenuAction("TexturesMaterial","StartMenu",m_Core));
	am.addAction(new LevelMenuAction("TexturesMaterial","StartMenu",m_Core));
	am.addAction(new TimeMenuAction("TexturesMaterial","StartMenu",m_Core));
	am.addAction(new GameOverMenuAction("TexturesMaterial","StartMenu",m_Core));

	RunState* rs = new RunState("Run",m_Core);
	//rs->setActive(false);
	gEngine->addGameState(rs);		
	gEngine->activateGameState("Run");
	//gEngine->getAudioManager().play("Opium",100,true);
	ds::WorldLayer wl = gEngine->getWorld().getLayerByName("HUD");
	gEngine->getWorld().deactivateLayer(wl);
	SetCursor(LoadCursor(NULL, IDC_ARROW));

	
}

//-----------------------------------------------------------------------------
// Name: wWinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE, LPWSTR, INT ) {	
	
#ifdef DEBUG
	FILE* pFile = fopen("application.log", "w");
	FileOutputHandler::Stream() = pFile;
	LOG(logINFO) << "Starting game";	
	pFile = fopen("application.log", "a");
	FileOutputHandler::Stream() = pFile;
#endif

	ds::Settings settings;
	settings.width = 1024;
	settings.height = 768;
	settings.title = "Blocks";
	settings.mode = 1;
	settings.synched = true;
	//settings.fullscreen = true;
	//settings.clearColor = ds::Color(0.87f,0.87f,0.87f,1.0f);
	settings.clearColor = ds::Color(0.0f,0.0f,0.0f,1.0f);
	settings.readPlainResources = true;
	Blocks *st = new Blocks();
	ds::CFramework *framework = new ds::CFramework(st);
	framework->initialize(hInstance,settings);
	LOG(logINFO) << "Running game now";
	framework->Run();
	LOG(logINFO) << "Game ended";
	framework->Release();
	delete framework;
	return 0; 
}
